package com.gitee.androidgoodies.batterylog;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.content.Context;
import android.widget.Toast;

public class SettingsLineNumActivity extends AppCompatActivity {
    private static final String TAG = "SettingsLineNumActivity";
    private RadioGroup mrgLineNumType;
    private EditText metLineNum;
    private boolean misLineNumEnable;
    private static Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LogUtils.d(TAG, "onCreate");
        mContext = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_line_num);
        Toolbar toolbar = findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);
        
        metLineNum = findViewById(R.id.activitysettingslinenumEditText1);
        metLineNum.setText(Integer.toString(BatteryLogConfigUtils.getConfigLineNum(this)));
        
        if(BatteryLogConfigUtils.getConfigIsLineNumEnable(this)){
           RadioButton rb = findViewById(R.id.activitysettingslinenumRadioButton1);
           rb.setChecked(true);
        } else {
            RadioButton rb = findViewById(R.id.activitysettingslinenumRadioButton2);
            rb.setChecked(true);
            metLineNum.setEnabled(false);
        }
        
        mrgLineNumType = (RadioGroup) findViewById(R.id.activitysettingslinenumRadioGroup1);
        mrgLineNumType.setOnCheckedChangeListener(new MyRadioButtonListener() ); 
    }
    
    public void onOK(View v){
        try {
            String szLineNum = metLineNum.getText().toString();
            int nLineNum = Integer.parseInt(szLineNum);
            if(nLineNum>-1){
                RecordService.setLineNum(nLineNum);
                BatteryLogConfigUtils.setConfigLineNum(this, nLineNum);
            }
            finish();
        } catch (NumberFormatException e) {
            LogUtils.d(TAG, e.getMessage());
        }
    }
    
    class MyRadioButtonListener implements OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.activitysettingslinenumRadioButton1:
                    metLineNum.setEnabled(true);
                    RecordService.setLineNumEnable(true);
                    BatteryLogConfigUtils.setConfigIsLineNumEnable(mContext, true);
                    break;
                case R.id.activitysettingslinenumRadioButton2:
                    metLineNum.setEnabled(false);
                    RecordService.setLineNumEnable(false);
                    BatteryLogConfigUtils.setConfigIsLineNumEnable(mContext, false);
                    break;
            }
        }
    }
}
